<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Env;
use app\common\model\ReplyAssetModel;
class UploadController extends Controller
{
    public function index()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(Env::get('ROOT_PATH') . 'public' . '/' . 'uploads/image/');
            if ($info) {
                return zbcjson([
                    'src' => '/uploads/image/' . $info->getSaveName()
                ]);
            } else {
                return zbcjson(false, $info->getError());
            }
        }
    }

    public function image($type)
    {
        $enum = ['news', 'case', 'product', 'slideshow'];
        if (!in_array($type, $enum)) {
            return zbcjson(false, "不存在当前分类");
        }
        $file = request()->file('file');
        if ($file) {

            $imgDir = '/uploads/image/' . $type . "/" . date("Ym") . "/";
            $dir = Env::get('ROOT_PATH') .'public'. $imgDir;
            if (!is_dir($dir)) {
                //第三个参数是“true”表示能创建多级目录，iconv防止中文目录乱码
                if (!mkdir($dir, 0777, true)) {
                    return zbcjson(false, "目录生成失败");
                }
            }
            $info = $file->rule('uniqid')->move($dir,md5($file));
            if ($info) {
                return zbcjson([
                    'src' => $imgDir . $info->getSaveName()
                ]);
            } else {
                return zbcjson(false, $info->getError());
            }
        } else {
            return zbcjson(false, "图片上传失败");
        }
    }

    public function wechatFile($type)
    {
        $file = request()->file('file');
        if ($file) {
            $imgDir = '/uploads/wechat/'.trim($type,"/")."/";
            $dir = Env::get('ROOT_PATH') .'public'. $imgDir;
            if (!is_dir($dir)) {
                //第三个参数是“true”表示能创建多级目录，iconv防止中文目录乱码
                if (!mkdir($dir, 0777, true)) {
                    return zbcjson(false, "目录生成失败");
                }
            }
            $info = $file->rule('uniqid')->move($dir,md5($file));
            if ($info) {
                $param=[
                    "name" => $info->getFilename(),
                    "extension" => $info->getExtension(),
                    "type" => $type,
                    "path" => $imgDir . $info->getSaveName()
                ];
                $asset=new ReplyAssetModel();
                $id=$asset->getIdBySave($param);

                if($type=="article"){
                    $config = \config("wechat.");
                    $wechat = new \WeChat\Media($config);
                    $filename=$_SERVER["DOCUMENT_ROOT"] .$param["path"];
                    try
                    {
                        $result = $wechat->uploadImg($filename);
                        $param["path"]=$result["url"];
                    }
                    catch(\Exception $ex){
                        return zbcjson(false, "上传微信素材失败");
                    }
                }

                return zbcjson([
                    'src' => $param["path"],
                    "id"=>$id
                ]);
            } else {
                return zbcjson(false, $info->getError());
            }
        } else {
            return zbcjson(false, "上传失败");
        }
    }
}
